<template>
  <div class="main flex_center">
    <div class="left">
      <div class="title_img"></div>

      <div class="goods_list">
        <div class="title flex_space_between">
          <div class="title_left">商品信息</div>
          <div class="title_right">共{{ goodsList.length }}种</div>
        </div>
        <el-scrollbar class="scroll_bar">
          <div
            class="item flex_center"
            v-for="(item, index) in goodsList"
            :key="index"
          >
            <div class="item_left">
              <img width="100%" height="100%" :src="item.image" />
            </div>
            <div class="item_right">
              <div class="title">{{ item.name }}{{ item.vip_price }}</div>
              <div class="code">{{ item.specifications }}</div>
              <div class="num flex_space_between">
                <div class="num_left">x{{ item.count }}</div>
                <div class="num_right">
                  <span>
                    {{
                      formatNumber((item.count * item.price).toFixed(2)).integer
                    }}
                  </span>
                  <span style="font-size: 11px">
                    {{
                      formatNumber((item.count * item.price).toFixed(2)).decimal
                    }}
                  </span>
                  <SPAN class="vip_icon">会员价</SPAN>
                  <span style="color: #9b5219">
                    {{
                      formatNumber((item.count * item.vip_price).toFixed(2))
                        .integer
                    }}
                  </span>
                  <span style="font-size: 11px; color: #9b5219">
                    {{
                      formatNumber((item.count * item.vip_price).toFixed(2))
                        .decimal
                    }}
                  </span>
                </div>
              </div>
            </div>
          </div>
        </el-scrollbar>
        <div class="total">
          合计
          <span class="txt">
            <span style="font-size: 16px">￥</span>
            <span> {{ formatNumber(totalCount.toFixed(2)).integer }} </span>
            <span style="font-size: 18px">
              {{ formatNumber(totalCount.toFixed(2)).decimal }}
            </span>
            <span class="vip_icon_total flex_center_center">会员价</span>
            <span style="color: #9b5219">
              {{ formatNumber(totalCountVip.toFixed(2)).integer }}
            </span>
            <span style="font-size: 18px; color: #9b5219">
              {{ formatNumber(totalCountVip.toFixed(2)).decimal }}
            </span>
          </span>
        </div>
      </div>
    </div>
    <div class="right">
      <div class="img" style="width: calc(100%-36px)"></div>
      <div class="qr_code_box">
        <div class="qr_code flex_center_center">
          <div class="qr_code_left flex_center_center">
            <div class="title">扫码下单 赢好礼</div>
            <div class="tip">不要排队就是好</div>
          </div>
          <div class="qr_code_right"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref, reactive, computed } from "vue";
import { useFormattedNumber } from "../../utils/common";
const { formatNumber } = useFormattedNumber();

const goodsList = reactive([]);

// 初始化 goodsList
const initializeGoodsList = () => {
  const storedList = localStorage.getItem("goodsList");
  if (storedList) {
    const parsedList = JSON.parse(storedList);
    goodsList.push(...parsedList);
    console.log(goodsList);
  }
};

// 监听 storage 事件
const onStorageChange = (event) => {
  if (event.key === "goodsList") {
    const newGoodsList = JSON.parse(event.newValue);
    goodsList.splice(0, goodsList.length, ...newGoodsList);
    console.log(goodsList);
  }
};

onMounted(() => {
  initializeGoodsList();
  window.addEventListener("storage", onStorageChange);
  console.log(goodsList);
});

// 计算总商品数
const totalNum = computed(() => goodsList.length);

// 计算所有商品的总价
const totalCount = computed(() => {
  return goodsList.reduce((sum, item) => sum + item.count * item.price, 0);
});
const totalCountVip = computed(() => {
  return goodsList.reduce((sum, item) => sum + item.count * item.vip_price, 0);
});
</script>

<style lang="scss" scoped>
@import "../../assets/style/common";
@import "../../assets/style/cashier";

.vip_icon,
.vip_icon_total {
  margin: 0 2px 0 10px;
  padding: 2px 4px;
  width: 42px;
  // height: 15px;
  background: linear-gradient(91deg, #ecc187 0%, #f8e5c0 50%, #ecc187 100%);
  border-radius: 6px;
  font-weight: bold;
  font-size: 11px;
  color: #9b5219;
}

.vip_icon_total {
  font-size: 18px !important;
  width: 72px !important;
  padding-bottom: 1px !important;
}

.main {
  width: 100%;
  height: 100%;
  background: #eef2f5;
  padding: 15px;

  .left,
  .right {
    border-radius: 6px;
    height: 100%;
  }

  .left {
    width: 35%;
    margin-right: 1%;

    .title_img {
      width: 100%;
      height: 14%;
      background: url("src/assets/images/结算.png") no-repeat center center;
      background-size: 100%;
      border-radius: 9px;
    }

    .goods_list {
      padding: 15px;
      height: 82%;
      width: 100%;
      border-radius: 6px;
      margin-top: 4%;
      background: white;

      .title {
        align-items: center;

        .title_left {
          font-weight: 800;
          font-size: 14px;
          color: #333333;
        }

        .title_right {
          font-weight: 400;
          font-size: 14px;
          color: #333333;
        }
      }

      .scroll_bar {
        margin-top: 2%;
        height: 88%;

        .item {
          padding-bottom: 14.5px;
          border-bottom: 1px solid #d9d9d9;

          .item_left {
            width: 68px;
            height: 68px;
            background: #d9d9d9;
            border-radius: 0px 0px 0px 0px;
            margin-right: 10px;
          }

          .item_right {
            flex: 1;

            .title {
              font-weight: 800;
              font-size: 14px;
              color: #333333;
            }

            .code {
              margin-top: 6.5px;
              font-weight: 400;
              font-size: 11px;
              color: #999999;
            }

            .num {
              margin-top: 6.5px;
              font-weight: 800;
              font-size: 14px;
              color: #333333;

              .num_right {
                display: flex;
                align-items: baseline;
              }
            }
          }
        }
      }

      .total {
        display: flex;
        justify-content: end;
        align-items: baseline;
        margin-top: 2%;
        font-weight: 800;
        font-size: 18px;
        color: #333333;

        .txt {
          display: flex;
          align-items: baseline;
          font-weight: 800;
          font-size: 24px;
          // color: #fd6013;
        }
      }
    }
  }

  .right {
    background: white;
    padding: 26px 18px;
    width: 64%;

    .img {
      height: 60%;
      background: #d9d9d9;
      margin-bottom: 2%;
    }

    .qr_code_box {
      display: flex;
      justify-content: end;
      height: 38%;

      .qr_code {
        width: 57%;
        height: 100%;

        .qr_code_left {
          flex-direction: column;
          margin-right: 38px;

          .title {
            font-weight: 800;
            font-size: 28px;
            color: #333333;
          }

          .tip {
            margin-top: 16.5px;
            font-weight: 800;
            font-size: 17px;
            color: #333333;
          }
        }

        .qr_code_right {
          width: 219px;
          height: 214px;
          border-radius: 11px 11px 11px 11px;
          background: yellow;
        }
      }
    }
  }
}
</style>
